var app = getApp();
const quote = app.globalData.quote;
const api = quote.api;
const tool = quote.tool;
const innerAudioContext = app.globalData.innerAudioContext; //引入音频播放器
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //   animation: null,//引导页动画
      isShowGuidance: true,//是否显示引导页
      taskStatus: app.globalData.taskStatus,//任务状态
      imgUrl:'',
      isBrang: app.globalData.isBrang,
      isShowTip: false,//是否通关完毕
    //   isShowEndTip: false,//任务结束弹窗
      taskThreeData:null,
      taskThreeAllData:null,
      isTrue:false,   //是否选对答案
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this
      that.endTip = this.selectComponent("#endTip");
      that.setData({
          taskStatus: app.globalData.taskStatus,//任务状态
      })
      that.getTaskThreeData()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      var that = this
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      return {
          path: 'pages/indexPackage/index/index',
          success: function (res) {
              console.log('转发成功', res);
          },
          fail: function (err) {
              console.log('转发失败', err)
          }
      }
  },
    beginTask:function(){
        var that=this
        that.setData({
            isShowGuidance: false,
        })
    },

    //获取任务三的数据
    getTaskThreeData: function () {
        var dataDic = {
            ModuleId: app.globalData.moduleId,
            TaskMark: 3
        };
        var that = this
        api.methodBasicEnglish_GetTaskData({
            dataDic: dataDic,
            success: function (res) {
                console.log(res)
                var data = res.data.data.TaskList
                data.forEach(item => {               
                    item.ImgUrl = app.globalData.imgUrl + item.ImgUrl
                })
                that.setData({
                    taskThreeData: data[0],
                    taskThreeAllData: data,
                    imgUrl:  data[0].ImgUrl
                })
                // that.setData({
                //     taskThreeData: res.data.data.TaskList[0],
                //     taskThreeAllData: res.data.data.TaskList, 
                //     imgUrl: app.globalData.imgUrl + res.data.data.TaskList[0].ImgUrl
                // })
            },
            fail:function(err){
                wx.showModal({
                    title: '温馨提示',
                    content: err.errMsg,
                })
            }
        })
    },
    //结束任务
    endTask: function () {
        var that = this
        that.endTip.showPopup(); 
    },
    //用户点击取消
    cancal: function () {
        var self = this
        self.endTip.hidePopup(); 
    },
    //用户点击确定
    confirm: function () {
        var that = this
        tool.statistics(app.globalData.openId, app.globalData.moduleId, 3, 3, 1)
        wx.redirectTo({
            url: '../../../../pages/indexPackage/courseList/courseList',
        })
    },
    //选择答案
    selectAnswer: function (e) {
        var that = this
        console.log(e, that.data.taskThreeData)
        var index = e.currentTarget.dataset.index
        var arr = 'taskThreeData.OptionList[' + index + '].Status'
      console.log('130', arr, that.data.taskThreeData.Answer)
        that.setData({
            [arr]: !that.data.taskThreeData.OptionList[index].Status,
        })
        if (index == that.data.taskThreeData.Answer) {
            wx.showToast({
                title: '答对了',
                icon: 'success',
                duration: 1000,
                mask: true
            })
            console.log('right')
            innerAudioContext.src = '/src/record/true1.mp3'
            innerAudioContext.play()
            that.setData({
                isTrue: true,
            })        
           that.data.taskThreeAllData.splice(0, 1)
            if (that.data.taskThreeAllData.length > 0) {
                // console.log('还没测完')
              console.log(that.data.taskThreeAllData,'144')
                setTimeout(() => {
                    console.log('还没测完')
                    that.setData({
                        taskThreeData: that.data.taskThreeAllData[0],
                        // imgUrl: app.globalData.imgUrl + that.data.taskThreeAllData[0].ImgUrl
                        imgUrl: that.data.taskThreeAllData[0].ImgUrl
                    })
                }, 1200)
            } else {
                console.log('测完')
                setTimeout(() => {
                    that.setData({
                        isShowTip: true,
                    })
                }, 1200)
            }

        } else {
            wx.showToast({
                title: '答错了',
                image: '/src/images/popup_close.png',
                duration: 1000,
                mask: true
            })
            console.log('err', that.data.taskThreeAllData)
            innerAudioContext.src = '/src/record/false.mp3'
            innerAudioContext.play()
            that.setData({
                isTrue: false,
            })
            setTimeout(() => {
                for (var j = 0; j < that.data.taskThreeData.OptionList.length; j++) {
                    if (that.data.taskThreeData.OptionList[j].Status) {
                        var arr = 'taskThreeData.OptionList[' + j + '].Status'
                        that.setData({
                            [arr]: false
                        })
                    }
                }
                that.data.taskThreeAllData.splice(0, 1)
                that.data.taskThreeAllData.push(that.data.taskThreeData)
                console.log(that.data.taskThreeAllData)
                that.setData({
                    taskThreeData: that.data.taskThreeAllData[0],
                    // imgUrl: app.globalData.imgUrl + that.data.taskThreeAllData[0].ImgUrl
                    imgUrl: that.data.taskThreeAllData[0].ImgUrl
                })
            }, 1200)

        }
    },
    //通关
    nextOne: function () {
        var that = this
        if (that.data.taskStatus.TaskFour == true) {
            wx.navigateTo({
                url: '/pages/taskPackage/pages/taskFour/taskFour',
            })
            return
        } else {

        }
    },
})